<template>
  <alarmevents class="" :title="title" @changeList="changeList" :data="alarmList" :type="1" :isshowlefts="isshowlefts"
    :timeList="timeList" :selectTime="selectTime" />
</template>

<script lang="ts" setup>
import alarmevents from '@/views/large-data-screen/component/public/alarmevents.vue'
import { getScreealarm, screenworkorder, screeninspect } from '@/api/screen/largeData'
import img1 from '@/assets/screenimg/samllIcon.png'
import img2 from '@/assets/screenimg/samllIcon1.png'
import img3 from '@/assets/screenimg/samllIcon3.png'
import { showAlarmList } from '@/views/large-data-screen/utils/index'
import { timeFormat } from '@/utils/functions'
import { emitter } from '@/utils/mitt'
const selectTime = ref(0) //选择时间
const isshowlefts = ref(true) //动画效果
const title = ref('告警事件')
const alarmEventDT = ref(1)
const clickTime = ref()
const clickTimes = ref()
// 时间选择
const timeList = ref([
  {
    name: '日',
    id: 1
  },
  {
    name: '周',
    id: 2

  },
  {
    name: '月',
    id: 3

  },
  {
    name: '年',
    id: 4

  }
])
const timeMap = {
  /** 本日 */
  today: timeFormat(new Date(), 'd'),
  /** 本周一 */
  monday: timeFormat(new Date(new Date().setDate(new Date().getDate() - new Date().getDay() + 1)), "d"),
  /** 本周日 */
  sunday: timeFormat(new Date(new Date().setDate(new Date().getDate() + (7 - new Date().getDay()))), "d"),
  /** 本月第一天 */
  firstDayOfMonth: timeFormat(new Date(new Date().getFullYear(), new Date().getMonth(), 1), "d"),
  /** 本月最后一天 */
  lastDayOfMonth: timeFormat(new Date(new Date().getFullYear(), new Date().getMonth() + 1, 0), "d"),
  /** 本年第一天 */
  firstDayOfYear: timeFormat(new Date(new Date().getFullYear(), 0, 1), "d"),
  /** 本年最后一天 */
  lastDayOfYear: timeFormat(new Date(new Date().getFullYear(), 11, 31), "d")
}
//告警事件列表
const alarmList = ref([
  {
    name: '已处理(起)',
    color: '#21CC31',
    number: 0,
    type: 1,
    img: img2,
    clickFun: () => showAlarmList({ defaultSearch: { searchType: 1, dealStatus: 3,createdAt:[clickTime.value, clickTimes.value],
                  startTime: clickTime.value,
                  endTime: clickTimes.value, } })
  },
  {
    name: '待确认(起)',
    number: 0,
    color: '#27E6E1',
    type: 2,
    img: img1,
    clickFun: () => showAlarmList({ defaultSearch: { searchType: 1, confirm: -1,createdAt:[clickTime.value, clickTimes.value],
                  startTime: clickTime.value,
                  endTime: clickTimes.value, } })
  },
  {
    name: '待处理(起)',
    number: 0,
    color: '#FF802B',
    type: 3,
    img: img3,
    clickFun: () => showAlarmList({ defaultSearch: { searchType: 1, dealStatus: -1,createdAt:[clickTime.value, clickTimes.value],
                  startTime: clickTime.value,
                  endTime: clickTimes.value, } })
  },

])
const re = (data) => {
  getAlarmData()
}
onMounted(() => {
  clickTime.value = timeMap.today
  clickTimes.value =  timeMap.today
  getAlarmData()//获取告警数据
  emitter.on('alarmEvents', re)
})
onBeforeUnmount(() => {
    emitter.off('alarmEvents',re)
})
//获取告警事件数据
const getAlarmData = () => {
  getScreealarm(
    {
      searchType: 1, //查询类型(1 单位 2 -个体概览)
      alarmConfirmDT: 0,//告警类型统计 （ 0-全部  3 本月 4 年）
      alarmEventDT: alarmEventDT.value,//告警事件 （ 0-全部 1 日 2周 3 月 4 年）
      systematicId: '',//应用系统id
      days: 7          //最近几日（  7，15）
    }
  ).then(res => {

    alarmList.value[0].number = res.result.alarmEvent.alarmDealed
    alarmList.value[1].number = res.result.alarmEvent.alarmWait
    alarmList.value[2].number = res.result.alarmEvent.alarmDealing
  })
}
//点击年月日
const changeList = (item, type, index) => {
  clickTime.value = null
  clickTimes.value =  null
  if(index == 0){
            // 今日
            clickTime.value = timeMap.today
            clickTimes.value =  timeMap.today
            console.log("日",clickTime.value,clickTimes.value);
            
          }
          if(index == 1){
            //周
            clickTime.value = timeMap.monday
            clickTimes.value = timeMap.sunday
            console.log("周",clickTime.value,clickTimes.value);
          }
          if(index == 2){
            
            //月
            clickTime.value = timeMap.firstDayOfMonth
            clickTimes.value = timeMap.lastDayOfMonth
            console.log("月",clickTime.value,clickTimes.value);
          }
          if(index == 3){
            //年
            clickTime.value = timeMap.firstDayOfYear
            clickTimes.value = timeMap.lastDayOfYear
            console.log("年",clickTime.value,clickTimes.value);
          }
  alarmEventDT.value = item.id
  selectTime.value = index
  isshowlefts.value = !isshowlefts.value
  setTimeout(() => {
    isshowlefts.value = !isshowlefts.value
    getAlarmData()
  }, 500);
}


</script>
